package com.ruoyi.business.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.business.domain.BusinessMessageTemplate;
import com.ruoyi.business.mapper.BusinessMessageTemplateMapper;
import com.ruoyi.business.service.IBusinessMessageTemplateService;

/**
 * 消息模板管理 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class BusinessMessageTemplateServiceImpl implements IBusinessMessageTemplateService 
{
    @Autowired
    private BusinessMessageTemplateMapper businessMessageTemplateMapper;

    /**
     * 查询消息模板信息
     * 
     * @param templateId 消息模板ID
     * @return 消息模板信息
     */
    @Override
    public BusinessMessageTemplate selectBusinessMessageTemplateById(Long templateId)
    {
        return businessMessageTemplateMapper.selectBusinessMessageTemplateById(templateId);
    }

    /**
     * 查询消息模板列表
     * 
     * @param businessMessageTemplate 消息模板信息
     * @return 消息模板集合
     */
    @Override
    public List<BusinessMessageTemplate> selectBusinessMessageTemplateList(BusinessMessageTemplate businessMessageTemplate)
    {
        return businessMessageTemplateMapper.selectBusinessMessageTemplateList(businessMessageTemplate);
    }

    /**
     * 通过模板代码查询消息模板
     * 
     * @param templateCode 模板代码
     * @return 消息模板对象
     */
    @Override
    public BusinessMessageTemplate selectBusinessMessageTemplateByCode(String templateCode)
    {
        return businessMessageTemplateMapper.selectBusinessMessageTemplateByCode(templateCode);
    }

    /**
     * 新增消息模板
     * 
     * @param businessMessageTemplate 消息模板信息
     * @return 结果
     */
    @Override
    public int insertBusinessMessageTemplate(BusinessMessageTemplate businessMessageTemplate)
    {
        return businessMessageTemplateMapper.insertBusinessMessageTemplate(businessMessageTemplate);
    }

    /**
     * 修改消息模板
     * 
     * @param businessMessageTemplate 消息模板信息
     * @return 结果
     */
    @Override
    public int updateBusinessMessageTemplate(BusinessMessageTemplate businessMessageTemplate)
    {
        return businessMessageTemplateMapper.updateBusinessMessageTemplate(businessMessageTemplate);
    }

    /**
     * 删除消息模板信息
     * 
     * @param templateId 消息模板ID
     * @return 结果
     */
    @Override
    public int deleteBusinessMessageTemplateById(Long templateId)
    {
        return businessMessageTemplateMapper.deleteBusinessMessageTemplateById(templateId);
    }

    /**
     * 批量删除消息模板信息
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBusinessMessageTemplateByIds(String ids)
    {
        return businessMessageTemplateMapper.deleteBusinessMessageTemplateByIds(Convert.toStrArray(ids));
    }

    /**
     * 修改消息模板状态
     * 
     * @param businessMessageTemplate 消息模板信息
     * @return 结果
     */
    @Override
    public int changeStatus(BusinessMessageTemplate businessMessageTemplate)
    {
        return businessMessageTemplateMapper.updateMessageTemplateStatus(businessMessageTemplate);
    }

    /**
     * 校验模板代码是否唯一
     * 
     * @param businessMessageTemplate 消息模板信息
     * @return 结果
     */
    @Override
    public String checkTemplateCodeUnique(BusinessMessageTemplate businessMessageTemplate)
    {
        Long templateId = StringUtils.isNull(businessMessageTemplate.getTemplateId()) ? -1L : businessMessageTemplate.getTemplateId();
        BusinessMessageTemplate info = businessMessageTemplateMapper.selectBusinessMessageTemplateByCode(businessMessageTemplate.getTemplateCode());
        if (StringUtils.isNotNull(info) && info.getTemplateId().longValue() != templateId.longValue())
        {
            return UserConstants.TEMPLATE_CODE_NOT_UNIQUE;
        }
        return UserConstants.TEMPLATE_CODE_UNIQUE;
    }
} 